
PlayersJoined = { }

require( "mysql" );

local epdb;
	
function ConnectToMySQL()

	local err;

	--Host, username, password, database name
	epdb, err = mysql.connect( "174.120.28.2", "lkm80tyx_epidemi", "K!Je:Ep<i+D$", "lkm80tyx_epidemic" );
	--epdb, err = mysql.connect( "174.132.165.130", "darkcybo_epidemi", "G+n}n18<k<DU:|{/GKc<:lJr%zL+Y;~BrdB", "darkcybo_epidemic" );
	
	if( epdb == 0 ) then
	
		Msg( "=== MYSQL ERROR === \n" .. err .. "\n" );
		epdb = nil;
	
	else
	
		Msg( "=== MYSQL CONNECTION ESTABLISHED === \n" );
	
	end

end

ConnectToMySQL();

function esc( str )

	return mysql.escape( epdb, str );

end

function sqlDoQuery( str )

	if( not epdb ) then 
	
		return { };
	
	end

	Msg( "QUERY EXECUTED: " .. str .. "\n" );

	local tbl, succ, err =  mysql.query( epdb, str );

	return tbl;

end

local CurSQLTable;
local CurSQLVars = { }

function sqlOpStart( table )

	CurSQLTable = table;
	CurSQLVars = { };

end

function sqlOpInsert()

	local sqlstr = "INSERT INTO `" .. CurSQLTable .. "` (";
	
	local comma = false;
	
	for k, v in pairs( CurSQLVars ) do
	
		if( comma ) then
		
			sqlstr = sqlstr .. ", ";
		
		end
		
		comma = true;
	
		sqlstr = sqlstr .. v.Name;
	
	end
	
	sqlstr = sqlstr .. ") VALUES (";
	
	local comma = false;
	
	for k, v in pairs( CurSQLVars ) do
	
		if( comma ) then
		
			sqlstr = sqlstr .. ", ";
		
		end
		
		comma = true;
	
		sqlstr = sqlstr .. "'" .. v.Val .. "'";
	
	end	
	
	sqlstr = sqlstr .. ")";
	
	sqlDoQuery( sqlstr );

end

function sqlOpAddVar( var, val )

	table.insert( CurSQLVars, { Name = mysql.escape( epdb, var ), Val = mysql.escape( epdb, val ) } );
	
end

function sqlTableHasEntryWithValue( table, field, value )

	local table = sqlDoQuery( "SELECT * FROM " .. table .. " WHERE " .. field .. " = '" .. value .. "'" );

	if( table and table[1] ) then return true; end
	
	return false;

end

------------------------------------------------------------
-- PLAYER / MYSQL INTERACTION
------------------------------------------------------------

local meta = FindMetaTable( "Player" );

function meta:sqlCharacterExists()

	local table = sqlDoQuery( "SELECT * FROM epi_characters WHERE Name = '" .. esc( self:RPNick() ) .. "' AND SteamID = '" .. self:SteamID() .. "'" );
	
	if( table and table[1] ) then return true; end
	
	return false;

end

function meta:sqlGetCharactersTable()

	local table = sqlDoQuery( "SELECT id, Name FROM epi_characters WHERE SteamID = '" .. self:SteamID() .. "'" );

	table = table or { };
	
	return #table, table;

end

function meta:sqlOpenSceneSendLoadableCharacters()

	local numchars, table = self:sqlGetCharactersTable();

	if( numchars > 0 ) then
	
		umsg.Start( "SCL", self );
			umsg.Short( numchars );
			for k, v in pairs( table ) do
				umsg.String( v[2] );
			end
		umsg.End();
	
	end

end

function meta:sqlAttemptLoadCharacter( id )

	local numchars, table = self:sqlGetCharactersTable();
	
	if( table and table[id] ) then
	
		local data = sqlDoQuery( "SELECT * FROM epi_characters WHERE SteamID = '" .. self:SteamID() .. "' AND id = '" .. table[id][1] .. "'" );
	
		if( data and data[1] ) then
	
			
			self:TransferToNewCharacter( data[1][2], data[1][4], data[1][5], data[1][6], data[1][7] );
	
			return true;
	
		end

	end	
	
	return false;

end

function meta:sqlCreateCharacterSave()

	if( not self:sqlCharacterExists() ) then
	
		local id = self:SteamID();
	
		sqlOpStart( "epi_characters" );
			sqlOpAddVar( "SteamID", id );
			sqlOpAddVar( "Class", self:GetPlayerClass() );
			sqlOpAddVar( "Name", self:RPNick() );
			sqlOpAddVar( "Age", self:GetPlayerAge() );
			sqlOpAddVar( "PhysDesc", self:GetPlayerPhysDesc() );
			sqlOpAddVar( "OriMdl", self:GetPlayerOriginalModel() );
		sqlOpInsert();		
	
	end

end

function meta:sqlHandleFirstTimeJoin()

	local id = self:SteamID();
	
	if( not PlayersJoined[id] ) then
	
		PlayersJoined[id] = true;
		
		if( not sqlTableHasEntryWithValue( "epi_users", "SteamID", id ) ) then
			
			sqlOpStart( "epi_users" );
				sqlOpAddVar( "SteamName", self:Nick() );
				sqlOpAddVar( "SteamID", id );
				sqlOpAddVar( "IP", self:IPAddress() );
				sqlOpAddVar( "Date", os.date( "%b-%d-%Y" ) );
			sqlOpInsert();
		
		end
	
	end

end
